package core
import grails.validation.ValidationException

class PersonController {
	static layout = 'country'
	def index () {
		def max =params.long('max')?:100

		def start = params.long('start')?:0

		render(view:'index', model: [persons: Person.list(max: max, offset:start),max: max, start: start])

		//render "<pre>hello: </pre>"

	}

	def show(long id){

		[person: Person.get(id)]
	}

	def save(long id){

		def person = new Person(params)

		if (person.save()){

			redirect(action: 'index')

			flash.content = "new person: ${person.id} was created"

		}
		else {
			render(view: 'createPer', model: [person: person])
			flash.error = "new person creation: Failed"
		}
	}
	def update (long id){

		def person =  Person.get(params.long('id'))
		person.properties = params
		if (person.save()){
			flash.content = "update succeeded"

			redirect(action:'index')

			
		}
		else {
			flash.error = "update faild"
			render(view:'editPer', model:[person: person])
		}
	}
	def editPer(){

		[person: Person.get(params.long('id'))]

	}

	def delete(long id){

		def person = Person.get(id)

		person.delete()

		redirect(view: 'index')
	}
	
}